package com.smartfast4j.manager.common.config;

import com.smartfast4j.manager.common.interceptor.LoginInterceptor;
import com.smartfast4j.manager.common.interceptor.NotEmptyInterceptor;
import com.smartfast4j.manager.common.interceptor.PermissionInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.*;

/**
 * springBoot2后，使用WebMvcConfigurer添加拦截器
 */
@Configuration
public class ManagerWebMvcConfigurer implements WebMvcConfigurer {

    /**
     * 权限拦截器
     */
    @Autowired
    private PermissionInterceptor permissionInterceptor;

    /**
     * 登录拦截器
     */
    @Autowired
    private LoginInterceptor loginInterceptor;

    /**
     * 非空校验拦截器
     */
    @Autowired
    private NotEmptyInterceptor notEmptyInterceptor;

    @Value("${local.fileUrlPathPattern}")
    private String fileUrlPathPattern; //上传到本地服务器的文件的访问映射地址

    /**
     * 配置拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 多个拦截器组成一个拦截器链
        // addPathPatterns 用于添加拦截规则
        // excludePathPatterns 用户排除拦截
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(
                        "/",
                        "/captcha.jpg",
                        "/sys/login",
                        "/sys/logout",
                        "/sysUser/**",
                        "/sys/tool/**",
                        "/fileManager/**",
                        "/css/**",
                        "/img/**",
                        "/js/**",
                        "/plugin/**",
                        "/fonts/**",
                        "/views/**",
                        "/sysCnArea/**",
                        "/cache/api/**",
                        "/cache/api/**",
                        fileUrlPathPattern
                );

        registry.addInterceptor(permissionInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(
                        "/",
                        "/captcha.jpg",
                        "/sys/login",
                        "/sys/logout",
                        "/sys/tool/**",
                        "/fileManager/**",
                        "/css/**",
                        "/img/**",
                        "/js/**",
                        "/plugin/**",
                        "/fonts/**",
                        "/views/**",
                        fileUrlPathPattern
                );

        //拦截所有请求
        registry.addInterceptor(notEmptyInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(
                        "/",
                        "/captcha.jpg",
                        "/css/**",
                        "/img/**",
                        "/js/**",
                        "/plugin/**",
                        "/fonts/**",
                        "/views/**",
                        fileUrlPathPattern
                );

    }

    /**
     * springboot2.0版本后配置拦截器会导致静态资源被拦截
     * 需要配置不拦截static目录下的静态资源
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }

    /**
     * 设置默认首页为views/login.html
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:/views/login.html");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);//设置成最高级
        WebMvcConfigurer.super.addViewControllers(registry);
    }
}
